#include "flash.h"

void fmc_erase_page(uint32_t pageAddr)
{
	fmc_unlock();
	fmc_flag_clear(FMC_FLAG_BANK0_END);
	fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
	fmc_flag_clear(FMC_FLAG_BANK0_PGERR);

	fmc_page_erase(pageAddr);

	fmc_flag_clear(FMC_FLAG_BANK0_END);
	fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
	fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
	fmc_lock();
}

void fmc_program(uint32_t pageAddr, uint8_t *data, uint16_t len)
{
	uint32_t addr = pageAddr;

	fmc_unlock();
	while(addr < pageAddr + len)
	{
		fmc_word_program(addr, *(uint32_t *)data);
		addr += 4;
		data += 4;
		fmc_flag_clear(FMC_FLAG_BANK0_END);
		fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
		fmc_flag_clear(FMC_FLAG_BANK0_PGERR); 
	}
	fmc_lock();
}
